package lbx.xskinlib;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Map;

import lbx.xskinlib.domain.ResType;

/**
 * @author lbx
 * @date 2017/11/6.
 */

public class ResourceManager {

    private static ResourceManager INSTANCE;
    private Map<String, Resources> mMap = new Hashtable<>();
    private Context mContext;
    private String mApkPath;
    private String mPkgName;
    private String mSuffix = "";

    static ResourceManager getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (ResourceManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new ResourceManager(context);
                }
            }
        }
        return INSTANCE;
    }

    private ResourceManager(Context context) {
        mContext = context.getApplicationContext();
    }

    private Resources getResources(String apkPath, String pkgName) {
        mApkPath = apkPath;
        mPkgName = pkgName;
        Resources resources = null;
        if (mApkPath != null) {
            resources = mMap.get(mApkPath);
        }
        return resources != null ? resources : createResources(mApkPath);
    }

    Resources getResources(String apkPath, String pkgName, String suffix) {
        mApkPath = apkPath;
        mPkgName = pkgName;
        mSuffix = suffix;
        Resources resources = null;
        if (!TextUtils.isEmpty(mApkPath)) {
            resources = mMap.get(mApkPath);
        }
        return resources != null ? resources : createResources(mApkPath);
    }

    private Resources createResources(String apkPath) {
        Resources resources;
        Resources res = mContext.getResources();
        try {
            if (TextUtils.isEmpty(apkPath)) {
                return res;
            }
            AssetManager assetManager = AssetManager.class.newInstance();
            Method method = assetManager.getClass().getMethod("addAssetPath", String.class);
            method.invoke(assetManager, apkPath);
            resources = new Resources(assetManager, res.getDisplayMetrics(), res.getConfiguration());
            mMap.put(apkPath, resources);
            return resources;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return res;
    }

    private Resources getDefaultResources() {
        return mApkPath == null && mPkgName == null ? mContext.getResources() : getResources(mApkPath, mPkgName);
    }

    public ColorStateList getColorByName(String resName) {
        Resources resources = getDefaultResources();
        Log.e("xys", getName(resName) + "  getColorByName  " + mPkgName);
        return resources.getColorStateList(resources.getIdentifier(getName(resName), ResType.DEFTYPE_COLOR, mPkgName));
    }

    public Drawable getBackgroundByName(String resName) {
        Resources resources = getDefaultResources();
        return resources.getDrawable(resources.getIdentifier(getName(resName), ResType.DEFTYPE_DRAWABLE, mPkgName));
    }

    public String getStringByName(String resName) {
        Resources resources = getDefaultResources();
        return resources.getString(resources.getIdentifier(getName(resName), ResType.DEFTYPE_TEXT, mPkgName));
    }

    public float getDimensionByName(String resName) {
        Resources resources = getDefaultResources();
        float density = resources.getDisplayMetrics().density;
        return resources.getDimension(resources.getIdentifier(getName(resName), ResType.DEFTYPE_DIMENSION, mPkgName)) / density;
    }

    @NonNull
    private String getName(String resName) {
        if (!TextUtils.isEmpty(mSuffix)) {
            return resName + "_" + mSuffix;
        }
        return resName;
    }
}
